using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AutoGear.BLL;
using ECommerce.Library.Mail;
using System.Collections.Specialized;
using ECommerce.Library;
using ECommerce.Library.Web;

public partial class register : SitePublicBasePage
{
    public String SupplierId
    {
        get
        {
            try
            {

                return Page.RouteData.Values["partnerId"].ToString();
            }
            catch
            {
                return string.Empty;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;
        if (!Page.IsPostBack)
        {

            ContentPlaceHolder ph = (ContentPlaceHolder)this.Master.Master.FindControl("cphMiddleContent");

            Literal litPageTitle = (Literal)ph.FindControl("litPageTitle");
            Literal litCurrentPage = (Literal)ph.FindControl("litCurrentPage");

            if (litPageTitle != null)
            {
                litCurrentPage.Text = litPageTitle.Text = "Register";

            }

            uccBillingState.DataBind();
            uccBillingCountry.DataBind();
            uccShippingState.DataBind();
            uccShippingCountry.DataBind();


        }
    }
    protected void imbRegister_Click(object sender, EventArgs e)
    {
        try
        {


            if (recaptcha1.IsValid)
            {
                //this.Page.Validate();

                string LoginEmailAddress = tbxPersonalEmailAddress.Text.Trim();
                if (!AutoGearSiteUser.IsUserExists(LoginEmailAddress))
                {
                    string firstName = Security.BindStringInput(Security.BindStringInput(tbxPersonalFirstName.Text, SecurityLevel.Two), SecurityLevel.Three);
                    string lastName = Security.BindStringInput(Security.BindStringInput(tbxPersonalLastName.Text, SecurityLevel.Two), SecurityLevel.Three);
                    DateTime dateOfBirth;
                    if (!DateTime.TryParse(tbxPersonalDateOfBirth.Text, out dateOfBirth))
                    {
                        uccResponseMessage.SetFailMessage("Incorrect date.");
                        uccResponseMessage.Visible = true;
                        return;
                    }
                    string emailAddress = tbxPersonalEmailAddress.Text.Trim();
                    if (!RegExUtil.IsEmail(emailAddress))
                    {
                        uccResponseMessage.SetFailMessage("Incorrect Personal Email.");
                        uccResponseMessage.Visible = true;
                        return;
                    }
                    Gender userGender;
                    if (rbtMale.Checked)
                    {
                        userGender = Gender.Male;
                    }
                    else
                    {
                        userGender = Gender.Female;
                    }
                    string userTelephoneNumber = tbxContactPhoneNumber.Text.Trim();
                    if (!RegExUtil.IsPhoneNumber(userTelephoneNumber))
                    {
                        uccResponseMessage.SetFailMessage("Incorrect Personal Phone Number.");
                        uccResponseMessage.Visible = true;
                        return;
                    }
                    string userFaxNumber = tbxContactFaxNumber.Text.Trim();
                    //if (!string.IsNullOrEmpty(userFaxNumber) && !RegExUtil.IsPhoneNumber(userFaxNumber))
                    //{
                    //    uccResponseMessage.SetFailMessage("Incorrect Personal Phone Number.");
                    //    uccResponseMessage.Visible = true;
                    //    return;
                    //}
                    string companyName = Security.BindStringInput(tbxCompanyName.Text, SecurityLevel.Three);
                    string companyTelephoneNumber = tbxCompanyPhoneNumber.Text.Trim();
                    //if (!RegExUtil.IsPhoneNumber(companyTelephoneNumber)) // Removed accorning to "Autogear Work Order 12-9-09.docx" -- simon
                    //{
                    //    uccResponseMessage.SetFailMessage("Incorrect Company Telephone Number.");
                    //    uccResponseMessage.Visible = true;
                    //    return;
                    //}
                    string companyFaxNumber = tbxCompanyFaxNumber.Text.Trim();
                    //if (!string.IsNullOrEmpty(companyFaxNumber) && !RegExUtil.IsPhoneNumber(companyFaxNumber))
                    //{
                    //    uccResponseMessage.SetFailMessage("Incorrect Company Fax Number.");
                    //    uccResponseMessage.Visible = true;
                    //    return;
                    //}

                    AutoGear.BLL.Address billingAddress = new AutoGear.BLL.Address(tbxBillingFirstName.Text, tbxBillingLastName.Text, tbxBillingAddressLine1.Text, tbxBillingAddressLine2.Text, tbxBillingCity.Text, uccBillingState.SelectedItem.Text, uccBillingCountry.SelectedItem.Text, tbxBillingZipCode.Text, tbxBillingPhoneNumber.Text, string.Empty, tbxBillingEmailAddress.Text);
                    AutoGear.BLL.Address shippingAddress = new AutoGear.BLL.Address(tbxShippingFirstName.Text, tbxShippingLastName.Text, tbxShippingAddressLine1.Text, tbxShippingAddressLine2.Text, tbxShippingCity.Text, uccShippingState.SelectedItem.Text, uccShippingCountry.SelectedItem.Text, tbxShippingZipCode.Text, tbxShippingPhoneNumber.Text, string.Empty, tbxShippingEmailAddress.Text);
                    string loginEmailAddress = tbxPersonalEmailAddress.Text.Trim();
                    if (!RegExUtil.IsEmail(loginEmailAddress))
                    {
                        uccResponseMessage.SetFailMessage("Incorrect Login Email.");
                        uccResponseMessage.Visible = true;
                        return;
                    }

                    string loginPassword = tbxLoginPassword.Text.Trim();
                    if (!RegExUtil.IsPassword(loginPassword))
                    {
                        uccResponseMessage.SetFailMessage("Incorrect Login Password.");
                        uccResponseMessage.Visible = true;
                        return;
                    }
                    bool isActive = true;
                    CustomerUser customerUser = CustomerUser.Create(firstName, lastName, dateOfBirth, emailAddress, userGender, userTelephoneNumber, userFaxNumber, companyName, companyTelephoneNumber, companyFaxNumber, billingAddress, shippingAddress, isActive, loginEmailAddress, loginPassword,string.Empty);
                    if (customerUser != null)
                    {
                        Email.SendRegistrationEmail(customerUser);
                        Email.SendRegistrationEmailToAdmin(customerUser);

                        //Returning User from PreCheckOut page, registers, auto logins  and returns to that page
                        if (SessionManager.ReturnUrl != null && AutoGearSessionManager.CheckoutSteps == CheckoutSteps.PreCheckout)
                        {
                            string returnUrl = SessionManager.ReturnUrl;
                            SessionManager.ReturnUrl = null;
                            if (AutoGearSiteUser.Login(LoginEmailAddress, loginPassword, UserLocationType.FrontEnd))
                            {
                                Response.Redirect(returnUrl);
                            }

                        }
                        else
                        {
                            Response.Redirect(string.IsNullOrEmpty(SupplierId) ? CustomSiteMap.RegistrationSuccessful : "~/partner/" + SupplierId + "/registration-successful.aspx");
                            
                        }


                    }
                    else
                    {
                        throw new Exception();
                    }
                }
                else
                {
                    uccResponseMessage.SetFailMessage("Another member found with this login email address.");
                    uccResponseMessage.Visible = true;
                }
            }
        }
        catch (Exception ex)
        {

            uccResponseMessage.SetFailMessage("Registration Failed.");
            uccResponseMessage.Visible = true;
        }
    }


}

